package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomizeException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 按条件分页查询菜品
     *
     * @param curPage
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<DishDto> queryDishDtoPage(Integer curPage, Integer pageSize, String name) {
        // 设置分页参数
        Page<DishDto> page = new Page<>(curPage, pageSize);
        return dishMapper.queryDishDtoPage(page, name);
    }

    /**
     * 根据菜品id查询菜品和菜品口味(嵌套查询)
     *
     * @param id
     * @return
     */
    @Override
    public DishDto queryDishDtoById(Long id) {
        return dishMapper.queryDishDtoById(id);
    }

    /**
     * 根据菜品id查询菜品和菜品口味(连接查询)
     *
     * @param id
     * @return
     */
    @Override
    public DishDto queryDishDtoByDishId(Long id) {
        return dishMapper.queryDishDtoByDishId(id);
    }


    /**
     * 修改菜品信息
     *
     * @param dishDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDishDto(DishDto dishDto) {
        // 更新菜品
//        this.updateById(dishDto);
        dishMapper.updateById(dishDto);
        // 删除菜品对应的所有口味
        dishFlavorService.lambdaUpdate().eq(DishFlavor::getDishId, dishDto.getId()).remove();
        // 批量新增菜品修改后的所有口味
        List<DishFlavor> dishFlavors = dishDto.getFlavors();
        if (!dishFlavors.isEmpty()) {
            dishFlavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
            dishFlavorService.saveBatch(dishFlavors);
        }
    }

    /**
     * 添加菜品和口味
     *
     * @param dishDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDishDto(DishDto dishDto) {
        // 添加菜品
        dishMapper.insert(dishDto);
        // 添加菜品的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        if (!flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));
            dishFlavorService.saveBatch(flavors);
        }
    }

    /**
     * 删除菜品以及菜品对应的口味
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteDishDto(List<Long> ids) {
        // 菜品关联的套餐菜品、购物车、订单详情
        Integer countSetMealDish = setmealDishService.lambdaQuery()
                .in(SetmealDish::getDishId, ids).count();
        if (countSetMealDish > 0) {
            throw new CustomizeException("菜品存在于套餐中，请先删除套餐");
        }
        Integer countShoppingCart = shoppingCartService.lambdaQuery()
                .in(ShoppingCart::getDishId, ids).count();
        if (countShoppingCart > 0) {
            throw new CustomizeException("菜品存在于购物车中，请先清空购物车");
        }
        Integer countOrderDetail = orderDetailService.lambdaQuery()
                .in(OrderDetail::getDishId, ids).count();
        if (countOrderDetail > 0) {
            throw new CustomizeException("菜品存在于订单详情中，请先删除订单详情");
        }
//        Integer countDishStatus = this.lambdaQuery().
//                eq(Dish::getStatus, 1).in(Dish::getId, ids).count();
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        Integer countDishStatus = dishMapper.selectCount(lqw);
        if (countDishStatus > 0) {
            throw new CustomizeException("有菜品正在出售，请先停售");
        }
        // 删除菜品的口味
        dishFlavorService.lambdaUpdate().in(DishFlavor::getDishId, ids).remove();
        // 删除菜品
        dishMapper.deleteBatchIds(ids);
    }

    /**
     * 根据分类id查询所有起售菜品
     * @param categoryId
     * @param name
     * @return
     */
    @Override
    public List<DishDto> queryDishDtoList(Long categoryId, String name, Integer status) {
        return dishMapper.queryDishDtoList(categoryId, name,status);
    }


    /**
     * 查询套餐中的菜品和份数
     * @param setMealId
     * @return
     */
    @Override
    public List<DishDto> queryDishDtoBySetMealId(Long setMealId) {
        return dishMapper.queryDishDtoBySetMealId(setMealId);
    }

}


